import { useStore } from 'vuex';
import { computed } from 'vue';

// 对 vue3 使用 mapState、mapGetters 的封装
export function useStoreMap(mapper, useMap) {
    // 获取 store 独享
    const store = useStore();
    // 获取对应的对象的function: {name: function, age: function}
    const storeStateFns = useMap(mapper);
    // 对数据进行转换
    const storeState = {};
    Object.keys(storeStateFns).forEach((Fnkey) => {
        // setup中无 this 指向,在 computed 中计算state时需要 $store 指向,所以使用 bind() 绑定 $store
        const fn = storeStateFns[Fnkey].bind({ $store: store });
        storeState[Fnkey] = computed(fn);
    });
    // 返回值
    return storeState;
}